<?php
/**
 *  ### Using the alias `i0' for the tree index table
 *
 *
 */
namespace Dclub\modules\Catalog;

class Action_SelectTreeToDepth
    extends Action_SelectTreeAbstract
{

    /**
     *  @var int ?
     */
    protected $maxDepth;


    /************************************
     *         Public methods          *
     ************************************/


    /**
     *  @param int $fromNodeId
     *  @return this
     */
    public function setMaxDepth( $depth )
    {
        debug();
        $this->maxDepth = \Yz_Int::testPositive($depth);
        return $this;
    }


    /************************************
     *       Protected methods          *
     ************************************/


    /**
     *  Modified SELECT statement creator
     *
     */
    protected function buildSelect()
    {
        debug();
        $sql = parent::buildSelect();
        $ti = self::TABLE_INDEX;

        $depth = $this->maxDepth;
        if (null !== $depth) {
            $sql->where("{$ti}.depth<={$depth}");
        }
        $sql
            ->where("{$ti}.parentId={$ti}.id") // depth select condition
            ->orderBy("{$ti}.depth") // TODO do we need ordering at all?
            ;
        return $sql;
    }


}
